import 'package:demo/util/asset.dart';
import 'package:flutter/material.dart';
import 'dart:convert';

class BannerCom extends StatefulWidget {
  @override
  _BannerComState createState() => _BannerComState();
}

class _BannerComState extends State<BannerCom> {
  var sumSpend, sumIncome;
  int surplus = 0;

  void initData() {
    String type = SpUtil.preferences.getString('totalType');
    bool isTab = false;
    isTab = SpUtil.preferences.getBool('isTab');

    var obj;
    if (type == '今天') {
      obj = jsonDecode(SpUtil.preferences.getString('thisDay'));
    }
    if (type == '本月') {
      obj = jsonDecode(SpUtil.preferences.getString('thisMonth'));
    }
    if (type == '本年' || isTab) {
      obj = jsonDecode(SpUtil.preferences.getString('thisYear'));
    }
    if (obj.length == 0) {
      sumIncome = 0;
      sumSpend = 0;
      surplus = 0;
    } else {
      sumIncome = obj['sumIncome'];
      sumSpend = obj['sumSpend'];
      surplus = sumIncome - sumSpend;
    }
  }

  Row textRow(String text, double fontSize, bool weight) {
    return Row(
      children: [
        new Text(
          text,
          style: TextStyle(
            fontSize: fontSize,
            fontWeight: weight ? FontWeight.bold : FontWeight.normal,
            color: Colors.white,
            letterSpacing: 0.5,
          ),
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    initData();
    return Container(
      padding: const EdgeInsets.all(20.0),
      height: 160.0,
      alignment: Alignment.bottomLeft,
      decoration: new BoxDecoration(
        image: DecorationImage(
          image: AssetImage('images/city.jpeg'),
          fit: BoxFit.cover,
        ),
      ),
      child: Column(mainAxisAlignment: MainAxisAlignment.end, children: [
        textRow('结余', 18.0, false),
        SizedBox(height: 4),
        textRow('$surplus元', 32.0, true),
        SizedBox(height: 4),
        textRow('收入 $sumIncome 丨 支出 $sumSpend', 14.0, false),
      ]),
    );
  }
}
